import { defineStore } from "pinia";

const _local_name = 't2i-settings'

export const useSettingsStore = defineStore('settings', {
    state: ():Settings.Settings => {
        const res = localStorage.getItem(_local_name)
        if (res) {
            return JSON.parse(res) as Settings.Settings
        }
        return {
            proxy: '',
            apiKey: ''
        }
    },
    getters: {

    },
    actions: {
        updateProxy(proxy: string) {
            if (this.proxy == proxy) return
            this.proxy = proxy
            this.recordState()
        },
        updateApiKey(apiKey: string) {
            if (this.apiKey == apiKey) return
            this.apiKey = apiKey
            this.recordState()
        },
        recordState() {
            localStorage.setItem(_local_name, JSON.stringify(this.$state))
        }
    }
})